package com.icoolkj.mall.user.openfeign.demo.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author icoolkj
 * @version 1.0
 * @description
 * @createDate 2025/01/23 15:56
 */
@Slf4j
public class FeignAuthRequestInterceptor implements RequestInterceptor{

    @Override
    public void apply(RequestTemplate template) {
        // 业务逻辑，模拟认证逻辑
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(null != requestAttributes){
            HttpServletRequest request = requestAttributes.getRequest();
            String access_token = request.getHeader("Authorization");
            log.info("从 Request 中解析请求头：{}", access_token);
            // 设置 token
            template.header("Authorization", access_token);
        }
    }
}
